package practice03;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Administrator
 * @version V1.0
 * @ProjectName:IntelliJ IDEA
 * @PackageName: practice03
 * @ClassName: LoginServer
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2019/12/30 18:43
 * @Copyright 大牛版板所有
 */
public class LoginServer {
    public static void main(String[] args) {
        ServerSocket loginServer = null;
        ObjectInputStream reader = null;
        PrintWriter out = null;
        Socket loginClient = null;
        String str = "bjsxt";
        try {
            loginServer = new ServerSocket(9000);
            System.out.println("服务器已经启动");
            loginClient = loginServer.accept();
            reader = new ObjectInputStream(loginClient.getInputStream());
            User user = (User)reader.readObject();

            out = new PrintWriter(loginClient.getOutputStream(),true);
            String reply = null;
            if (user.getUsername().equals(str) && user.getPassword().equals(str)){
                reply = "正确";
                System.out.println("密码与账户正确");
            }
            else {
                reply = "错误";
                System.out.println("密码或账户错误");
            }
            out.println(reply);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (loginClient != null){
                try {
                    loginClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
